﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HYRElectric.Equipment
{
	public class ReferenceUpdater : IUpdater
	{
		public ReferenceUpdater(AddInId addinID)
		{
			this.m_updaterId = new UpdaterId(addinID, new Guid("6B8C1A6B-C30A-43BF-9694-84F03E63A589"));
		}

		public void Execute(UpdaterData data)
		{
			string text = string.Empty;
			string empty = string.Empty;
			Document document = data.GetDocument();
			try
			{
				foreach (ElementId elementId in data.GetModifiedElementIds())
				{
					Element element = document.GetElement(elementId);
					Schema schema = Schema.Lookup(new Guid("720080CB-DA99-40DC-9415-E53F280AA3F0"));
					if (schema != null)
					{
						Entity entity = element.GetEntity(schema);
						if (entity.Schema != null)
						{
							IDictionary<string, string> dictionary = entity.Get<IDictionary<string, string>>("referencePlane");
							string text2 = dictionary.Keys.FirstOrDefault<string>();
							if (!(text2 != elementId.IntegerValue.ToString()))
							{
								string text3 = dictionary[text2];
								string[] array = text3.Split(new char[]
								{
									','
								});
								XYZ xyz = this.StringToXYZ(text3.Split(new char[]
								{
									','
								})[0]);
								double num = Convert.ToDouble(text3.Split(new char[]
								{
									','
								})[1]);
								XYZ point = (element.Location as LocationPoint).Point;
								XYZ handOrientation = (element as FamilyInstance).HandOrientation;
								new XYZ(1.0, 0.0, 0.0).AngleTo(handOrientation.Normalize());
								XYZ moveVector = point.Subtract(xyz);
								for (int i = 2; i < array.Length; i++)
								{
									ElementId elementId2 = new ElementId(Convert.ToInt32(array[i]));
									Element element2 = document.GetElement(elementId2);
									if (element2 != null && element2.IsValidObject)
									{
										new YJKRotateFamilyInstance().MoveElement(moveVector, element2);
									}
								}
								IDictionary<string, string> dictionary2 = new Dictionary<string, string>();
								text = SwitchBoxesLabel.PointToString(point);
								array[0] = text;
								dictionary2.Add(text2, array.Join(","));
								SwitchBoxesLabel.StoreExtData(element, dictionary2);
								document.Regenerate();
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
		}

		private XYZ StringToXYZ(string value)
		{
			XYZ result = null;
			if (string.IsNullOrEmpty(value))
			{
				return result;
			}
			string[] array = value.Split(new char[]
			{
				'+'
			});
			double num = Convert.ToDouble(array[0]);
			double num2 = Convert.ToDouble(array[1]);
			double num3 = Convert.ToDouble(array[2]);
			return new XYZ(num, num2, num3);
		}

		public string GetAdditionalInformation()
		{
			return "";
		}

		public ChangePriority GetChangePriority()
		{
			return 15;
		}

		public UpdaterId GetUpdaterId()
		{
			return this.m_updaterId;
		}

		public string GetUpdaterName()
		{
			return "";
		}

		private UpdaterId m_updaterId;
	}
}
